﻿Public Class WfCadServiço
    Inherits System.Web.UI.Page
    Dim despLogado As New Despachante

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        despLogado = Session("DespLogado")
     
        If IsPostBack = False Then
            If Request.QueryString("cod_serv") IsNot Nothing Then
                Dim servico As New Servico
                Dim servicoDao As New ServicoDAO
                Dim cod_serv As Integer = Request.QueryString("cod_serv")
                editar.Visible = True
                servico = servicoDao.SelectServId(cod_serv)
                tbDesc.Enabled = False
                tbValor.Enabled = False
                tbDesc.Text = servico.DescServ
                tbValor.Text = servico.ValorServ

            End If
        End If
       

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Request.QueryString("cod_serv") IsNot Nothing Then
            Dim servico As New Servico
            Dim servicoDao As New ServicoDAO
            Dim cod_serv As Integer = Request.QueryString("cod_serv")
            Try
                servico.CodServ = cod_serv
                servico.DescServ = tbDesc.Text
                servico.ValorServ = tbValor.Text

                servicoDao.Atualizar(servico)

                lbmgs.ForeColor = Drawing.Color.Green
                lbmgs.Text = "Serviço atualizado com Sucesso !"
                tbDesc.Enabled = False
                tbValor.Enabled = False
            Catch ex As Exception
                lbmgs.ForeColor = Drawing.Color.Red
                lbmgs.Text = "Erro ao Atualizar Serviço"
            End Try
        
        Else

            Dim objServ As New Servico
            Dim objServDao As New ServicoDAO

            Try
                objServ.Depachante = despLogado
                objServ.DescServ = tbDesc.Text
                objServ.ValorServ = tbValor.Text

                objServDao.Inserir(objServ)
                lbmgs.ForeColor = Drawing.Color.Green
                lbmgs.Text = "Serviço salvo com sucesso !"
                tbDesc.Enabled = False
                tbValor.Enabled = False
            Catch ex As Exception
                lbmgs.ForeColor = Drawing.Color.Red
                lbmgs.Text = "Erro ao Salvar serviço !"
            End Try

        End If

    End Sub


    Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        If Session("DespLogado") Is Nothing Then
            Response.Redirect("/Screens/WF1.aspx")
        End If
    End Sub

    Protected Sub editar_Click(sender As Object, e As EventArgs) Handles editar.Click
        tbDesc.Enabled = True
        tbValor.Enabled = True
    End Sub
End Class